package data.nanjing.expo.services;

import com.itextpdf.text.pdf.StandardDecryption;
import com.mysql.cj.util.Base64Decoder;
import data.nanjing.expo.common.DateTimeUtil;
import data.nanjing.expo.common.ExpoConstants;
import data.nanjing.expo.common.StringUtil;
import data.nanjing.expo.common.Util;
import data.nanjing.expo.ctrls.BaseCtrl;
import data.nanjing.expo.models.dao.ExhibitionDao;
import data.nanjing.expo.models.domain.FileManage;
import data.nanjing.expo.models.domain.dto.exhibition.*;
import data.nanjing.expo.models.domain.exhibition.*;
import org.apache.ibatis.javassist.bytecode.CodeAttribute;
import org.apache.poi.util.SystemOutLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.xml.crypto.Data;
import java.util.*;

/**
 * 展览中心
 *
 * @author ChanJi
 * @create 2018-06-24 9:56
 **/
@Service
public class ExpoService extends BaseCtrl {
    @Autowired
    ExhibitionDao exhibitionDao;

    /**********************************直出页面***************************************/

    /**
     * 分页显示展会信息
     *
     * @param page
     * @param limit
     * @return
     */
    public Map<String, Object> getExhibitionForRender(Integer page, Integer limit) {
        Map<String, Object> map = new HashMap<>();
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 12;
        }
        if (page < 1 || limit < 1) {
            map.put("ERROR", "请输入正确的页码和每页显示个数");
        }
        List<Exhibition> expo = exhibitionDao.getExhibitionList((page - 1) * limit, limit + 1);
        //判断时候有下一页
        if (expo.size() == limit + 1) {
            map.put("hasMore", true);
            map.put("nextPage", page + 1);
            expo = expo.subList(0, limit);
        } else {
            map.put("hasMore", false);
        }
        map.put("expo", expo);
        return map;
    }

    /**
     * 根据展会的id获取展会基本信息
     *
     * @param es
     * @param request
     * @return
     */
    public Map<String, Object> getExhibitionDetailForRender(ExpoScreenDto es, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        //展会id
        Integer id = es.getId();
        if (id < 0) {
            map.put("ERROR", "请输入正确的展会id");
            return map;
        }

        //展会的基本信息
        ExhibitionDto exhibitionInfo = exhibitionDao.getDetail(id);
        //展会下的活动
        List<ExhibitionActivity> exhibitionActivities = exhibitionDao.getActivity(id);
        //展会历史
        List<ExhibitionHistory> historyExhibitions = exhibitionDao.getHisExhibition(id);
        map.put("expoDetail", exhibitionInfo);
        map.put("activity", exhibitionActivities);
        map.put("hisExpo", historyExhibitions);
        //展馆id和展区名称
        Integer hallId = es.getHallId();
        Integer areaId = es.getAreaId();
        //如果展馆id为空则将展馆id设置为第一个展馆，展区id设置为第一个展馆的第一个展区
        if (hallId == null) {
            hallId = exhibitionDao.getExhibitionHallById(es.getId());
            areaId = exhibitionDao.getExhibitionAreaByHallId(hallId);
            if (hallId == null || areaId == null) {
                return map;
            } else {
                es.setHallId(hallId);
                es.setAreaId(areaId);
            }
        }
        //如果展区的id为空，则设置展区id为展馆的第一个展区
        if (areaId == null) {
            areaId = exhibitionDao.getExhibitionAreaByHallId(hallId);
            if (areaId == null) {
                return map;
            } else {
                es.setAreaId(areaId);
            }
        }
        //筛选条件
        List<ExhibitionHall> screen = getScreen(es, request);
        //根据帅选条件返回展位和对应的参展商
        Map<String, Object> stand = getParticipantDto(es);
        map.putAll(stand);
        map.put("screen", screen);
        return map;
    }

    /**
     * 获得展商在线筛选条件
     *
     * @param es
     * @param request
     * @return
     */
    private List<ExhibitionHall> getScreen(ExpoScreenDto es, HttpServletRequest request) {
        String url = request.getRequestURL().toString();
        Integer hallId = es.getHallId();
        Integer areaId = es.getAreaId();

        if (request.getQueryString() != null) {
            url = Util.buildQuery(url, "id", es.getId().toString());
        }
        //所有展馆id、名称、颜色
        List<ExhibitionHall> halls = exhibitionDao.getExhibitionHall(es.getId());
        //所有展区id，名称，展馆id
        List<ExhibitionArea> areas = exhibitionDao.getExhibitionArea(es.getId());

        //设置展馆和展区的url和选中状态，同时将展区加到展馆当中，返回嵌套格式的数据
        for (ExhibitionHall e : halls) {
            e.setUrl(Util.buildQuery(url, "hallId", e.getId().toString()));
            if (hallId.equals(e.getId())) {
                e.setSelect(true);
            } else {
                e.setSelect(false);
            }
            for (ExhibitionArea ea : areas) {
                if (ea.getHallId().equals(e.getId())) {
                    MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
                    params.add("hallId", e.getId().toString());
                    params.add("areaId", ea.getId().toString());
                    ea.setUrl(Util.buildQueryMulti(url, params));
                }
                if (areaId.equals(ea.getAreaId())) {
                    ea.setSelect(true);
                } else {
                    ea.setSelect(false);
                }
                //如果展区在展馆下则将展区加到展馆的展区list中
                if (e.getId().equals(ea.getHallId())) {
                    e.getArea().add(ea);
                }
            }
        }
        return halls;
    }

    /**
     * 返回展位信息
     *
     * @param es
     * @return
     */
    private Map<String, Object> getParticipantDto(ExpoScreenDto es) {

        Integer hallId = es.getHallId();
        Integer areaId = es.getAreaId();
        Integer page = es.getPage();
        Integer limit = es.getLimit();
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 30;
        }
        Integer offset = (page - 1) * limit;
        //查询展馆下的展位和公司信息
        List<ParticipantDto> participantDtos = exhibitionDao.getParticipantDto(hallId, areaId, offset, limit + 1);
        Map<String, Object> map = new HashMap<>();
        if (participantDtos.size() == limit + 1) {
            map.put("hasMore", true);
            map.put("nextPage", page + 1);
            participantDtos.subList(0, limit);
        } else {
            map.put("hasMore", false);
        }
        map.put("stands", participantDtos);
        return map;
    }

    /**
     * 根据参展商id获取参展商详细信息
     *
     * @param id
     * @return
     */
    public Map<String, Object> getExpoParticipant(Integer id) {
        Map<String, Object> map = new HashMap<>();
        if (id == null || id < 1) {
            map.put("ERROR", "请输入正确的参展商id");
            return map;
        }
        List<ParticipantDetailDto> participantDetailDtos = exhibitionDao.getExpoParticipant(id);
        map.put("participantDetail", participantDetailDtos);
        return map;
    }

/******************************************个人中心接口********************************************/

    /**
     * 上传展会基本信息
     *
     * @param expo
     * @return
     */
    @Transactional
    public Map<String, Object> uploadBaseInfo(UploadExpoDto expo) {
        Integer id = null;

        expo.setCreateTime(new Date());
        expo.setUpdateTime(new Date());

        //上传展会基本信息
        exhibitionDao.insertExhibition(expo);
        id = expo.getId();

        //插入展会基本信息
        exhibitionDao.insertExhibitionDetail(expo);

        Map<String, Object> map = new HashMap<>();
        map.put("expoId", id);
        return map;
    }


    /**
     * 判断展会名称是否重复
     *
     * @param expoName
     * @return
     */
    public void expoRepeat(String expoName) {
        Integer expoId;
        try {
            expoId = exhibitionDao.getExpoIdByName(expoName);
            if (expoId != null) {
                throw new RuntimeException("展会名称重复");
            }
        } catch (Exception e) {
            throw new RuntimeException("展会名称重复");
        }
    }

    /**
     * 上传展位信息
     *
     * @param hallDtos
     * @param expoId
     */
    @Transactional
    public Map<String, Object> saveOrUpdateStandInfo(List<HallDto> hallDtos, Integer expoId) {
        //获取 expoId 下的所有展位信息，不为空即为修改，为空即为新增
        Boolean isUpdate = false;
        Map<String, Object> map = new HashMap<>();
        List<StandDto> allStands = exhibitionDao.getAllStandByExpoId(expoId);
        List<StandDto> newStands = new ArrayList<>();

        List<String> notValidVerifyCode = new ArrayList<>();
        //缺失展位的参展商
        List<String> notValidCompany = new ArrayList<>();
        if (allStands.size() > 0) {
            isUpdate = true;
            //删除所有展区展位信息
            exhibitionDao.delStands(expoId);
        }
        List<String> hallColors = exhibitionDao.getHallColors();
        int i = 0;
        //插入展馆信息
        for (HallDto h : hallDtos) {
            h.setExpoId(expoId);
            h.setUpdateTime(new Date());
            if (h.getId() == null) {
                //获得展馆的所有颜色
                if (i == hallColors.size() - 1) {
                    i = 0;
                }
                h.setHallColor(hallColors.get(i));
                h.setCreateTime(new Date());
                exhibitionDao.insertExhibitionHall(h);
                i++;
            } else {
                //更新展厅信息
                exhibitionDao.updateExhibitionHall(h);
            }

            //插入展区信息
            for (AreaDto a : h.getArea()) {
                a.setExpoId(expoId);
                a.setHallId(h.getId());
                a.setUpdateTime(new Date());
                a.setCreateTime(new Date());
                exhibitionDao.insertExhibitionArea(a);
                //插入展位信息
                for (StandDto s : a.getStand()) {
                    //插入展位信息
                    for (int j = s.getStart(); j <= s.getEnd(); j++) {
                        //展会id和长度为10的随机数字加字母的密码拼接
                        s.setExpoId(expoId);
                        s.setHallId(h.getId());
                        s.setAreaId(a.getId());
                        s.setUpdateTime(new Date());
                        s.setCreateTime(new Date());
                        s.setNumber(j);
                        if (!isUpdate) {
                            s.setVerifyCode(expoId.toString() + Util.getVerifyRandom(10));
                            exhibitionDao.insertExhibitionStand(s);
                        } else {
                            StandDto newStand = new StandDto();
                            newStand.setExpoId(s.getExpoId());
                            newStand.setHallId(s.getHallId());
                            newStand.setAreaId(s.getAreaId());
                            newStand.setWord(s.getWord());
                            newStand.setNumber(s.getNumber());
                            newStand.setUpdateTime(new Date());
                            newStand.setCreateTime(new Date());
                            //优先填充已经使用的验证码
                            StandDto sd = this.findStandVerifyCode(allStands, s, true);
                            if (sd != null) {
                                newStand.setVerifyCode(sd.getVerifyCode());
                                newStand.setUseCodeUserId(sd.getUseCodeUserId());
                                sd.setVerifyCode(null);
                            } else {
                                sd = this.findStandVerifyCode(allStands, s);
                                if (sd != null) {
                                    newStand.setVerifyCode(sd.getVerifyCode());
                                    sd.setVerifyCode(null);
                                } else {
                                    newStand.setVerifyCode(expoId.toString() + Util.getVerifyRandom(10));
                                }
                            }
                            //填充已经分配展位的参展商
                            sd = this.findStandParticipant(allStands, s);
                            if (sd != null) {
                                newStand.setParticipantId(sd.getParticipantId());
                                sd.setParticipantId(0);
                            }
                            exhibitionDao.insertExhibitionStand(newStand);
                            newStands.add(newStand);
                        }
                    }
                }
            }
        }
        if (isUpdate) {
            for (StandDto s1 : allStands) {
                if (s1.getVerifyCode() != null) {
                    notValidVerifyCode.add(s1.getVerifyCode());
                }
                if (s1.getParticipantId() != null && s1.getParticipantId() != 0) {
                    notValidCompany.add(s1.getParticipantId().toString());
                }
            }
        }
        map.put("notValidVerifyCode", notValidVerifyCode);
        map.put("notValidParticipant", notValidCompany);
        return map;
    }

    private StandDto findStandParticipant(List<StandDto> list, StandDto stand) {
        for (StandDto sd : list) {
            if (
                sd.getHallId() == stand.getHallId()
                &&
                sd.getWord().equals(stand.getWord())
                &&
                sd.getNumber() == stand.getNumber()
                &&
                sd.getParticipantId() != null
                &&
                sd.getParticipantId() != 0
            ) {
                return sd;
            }
        }
        return null;
    }

    private StandDto findStandVerifyCode(List<StandDto> list, StandDto stand) {
        for (StandDto sd : list) {
            if (
                sd.getVerifyCode() != null
            ) {
                return sd;
            }
        }
        return null;
    }

    private StandDto findStandVerifyCode(List<StandDto> list, StandDto stand, Boolean isUsed) {
        for (StandDto sd : list) {
            if (
                sd.getVerifyCode() != null
                &&
                sd.getUseCodeUserId() != null
            ) {
                return sd;
            }
        }
        return null;
    }

    /**
     * 上传历史展览信息
     *
     * @param e
     */
    @Transactional(rollbackFor = Exception.class)
    public void uploadExhibitionHistory(List<ExhibitionHistory> e, Integer expoId) {
        for (ExhibitionHistory h : e) {
            if (!h.getHisExpoUrl().contains("chinaexpo365.com")) {
                throw new RuntimeException("请输入本网站上的展会网页链接地址");
            }
            try {
                exhibitionDao.insertExhibitionHistory(expoId, h.getHisExpoName(), h.getHisExpoUrl(), h.getHisExpoId(), new Date(), new Date());
            } catch (Exception e1) {
                throw new RuntimeException("上传历史展会失败");
            }
        }

    }


    /**
     * 上传展会活动
     *
     * @param e
     * @param expoId
     */
    @Transactional(rollbackFor = Exception.class)
    public void uploadExhibitionActivity(List<ExhibitionActivity> e, Integer expoId) {
        for (ExhibitionActivity ea : e) {
            try {
                exhibitionDao.insertExhibitionActivity(expoId, ea.getActivityName(), ea.getActivityUrl(), ea.getActivityId(), new Date(), new Date());
            } catch (Exception e1) {
                throw new RuntimeException("上传展会活动失败");
            }
        }

    }

    /**
     * 根据用户id获得指定状态的展会
     *
     * @param userId
     * @return
     */
    public Map<String, Object> getUserExpoList(Integer userId) {
        Map<String, Object> map = new HashMap<>();
        List<ExpoOfUser> list = exhibitionDao.findExpoList(userId);
        map.put("expo", list);
        return map;
    }

    /**
     * 取消展会
     *
     * @param expoId
     * @param userId
     */
    public void cancelExpo(Integer expoId, Integer userId) {
        //查询展会时候存在
        Integer id = exhibitionDao.findExpo(expoId, userId);
        if (id == null) {
            throw new RuntimeException("取消展览失败");
        }
        //取消展会
        try {
            exhibitionDao.userCancelExpo(expoId);
        } catch (Exception e) {
            throw new RuntimeException("取消展会失败");
        }
    }

    /**
     * 下载展会的验证码
     *
     * @param expoId
     * @return
     */
    public List<String> getVerifyCode(Integer expoId) {
        List<String> list = exhibitionDao.getVerifyCode(expoId);
        return list;
    }


    /**
     * 参展商登陆
     *
     * @param verifyCode
     * @param userId
     */
    public ExpoOfUser participantLogin(String verifyCode, Integer userId) {

        //通过用户id查询是否已经登陆过
        Integer usedId;
        try {
            usedId = exhibitionDao.getIdByUserId(userId);
        } catch (Exception e) {
            throw new RuntimeException("已经登陆过不能再登陆");
        }
        if (usedId != null) {
            throw new RuntimeException("已经登陆过不能再登陆");
        }

        //验证验证码是否被使用过
        Integer standId;
        try {
            standId = exhibitionDao.verifyParticipantLogin(verifyCode);
        } catch (Exception e) {
            throw new RuntimeException("验证码重复");
        }
        if (Objects.isNull(standId)) {
            throw new RuntimeException("请联系主办方，获取最新的验证码");
        }

        //通过验证，将信息存入数据库，并返回展会名称和展会id
        try {
            exhibitionDao.updateVerifyCodeState(userId, standId);
            ExpoOfUser e = exhibitionDao.findExpoByStandId(standId);
            return e;
        } catch (Exception e) {
            throw new RuntimeException("登陆失败,信息上传失败");
        }
    }

    /**
     * 创建参展商
     *
     * @param c
     * @param userId
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer createParticipant(ParticipantDetailDto c, Integer userId) {
        Integer expoId = c.getExpoId();
        Integer hallId = c.getHallId();
        String areaName = c.getAreaName();
        String standWord = c.getStandWord();
        Integer standNumber = c.getStandNumber();
        String companyAbbreviation = c.getCompanyAbbreviation();
        String companyImg = c.getCompanyImg();
        Participants participants = new Participants(userId, companyAbbreviation, companyImg, expoId);
        Integer participantId;
        //验证参展商时候已经登陆
        Integer repeat;
        try {
            repeat = exhibitionDao.verifyParticipant(expoId, userId);
        } catch (Exception e) {
            throw new RuntimeException("上传参展商基本信息失败");
        }
        if (repeat != null) {
            throw new RuntimeException("已经入驻展会");
        }
        //上传参展商基本信息并返回参展商id
        try {
            participantId = exhibitionDao.insertParticipant(participants);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("上传参展商基本信息失败");
        }
        if (participantId == null) {
            throw new RuntimeException("上传参展商基本信息失败，未返回参展商id");
        }
        //上传参展商详细信息
        String companyName = c.getCompanyName();
        String address = c.getAddress();
        String companyScope = c.getCompanyScope();
        String officialUrl = c.getOfficialUrl();
        String companyFax = c.getCompanyFax();
        String companyIntroduce = c.getCompanyIntroduction();
        String caseIntroduce = c.getCaseIntroduction();
        String companyRecruit = c.getCompanyRecruit();
        String serviceIntroduction = c.getServiceIntroduction();
        String contacts = c.getContacts();
        String contactsPhone = c.getContactsPhone();
        String qq = c.getQq();
        String weChat = c.getWechat();
        String contactsEmail = c.getContactsEmail();
        ParticipantDetail participantDetail = new ParticipantDetail(
                participantId,
                companyName,
                address,
                companyScope,
                officialUrl,
                companyFax,
                companyIntroduce,
                caseIntroduce,
                companyRecruit,
                serviceIntroduction,
                contacts,
                contactsPhone,
                qq,
                weChat,
                contactsEmail);
        //插入参展商信息信息
        try {
            exhibitionDao.insertParticipantDetail(participantDetail);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("上传参展商详情失败!");
        }

        //将参展商展馆id,展区名称，展位名称存到对应的展位中去
        try {
            exhibitionDao.insertParticipantToStand(hallId, areaName, standWord, standNumber, standWord + standNumber);
            return participantId;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("上传展商详情失败,将参展商信息插入展位失败");
        }
    }


    /**
     * 根据展会id获取所有展馆名称和id
     *
     * @param expoId
     * @return
     */
    public Map<String, Object> getHallByExpoId(Integer expoId) {
        Map<String, Object> map = new HashMap<>();
        List<HallDto> list = exhibitionDao.getHallByExpoId(expoId);
        map.put("hall", list);
        return map;
    }

    /**
     * 通过展馆id获取展区名称和id
     *
     * @param hallId
     * @return
     */
    public Map<String, Object> getAreaByHallId(Integer hallId) {
        Map<String, Object> map = new HashMap<>();
        List<AreaDto> area = exhibitionDao.getAreaByHallId(hallId);
        map.put("area", area);
        return map;
    }

    /**
     * 通过展区id获得展位信息
     *
     * @param areaId
     * @return
     */
    public Map<String, Object> getStand(Integer areaId) {
        Map<String, Object> map = new HashMap<>();
        List<StandDto> stand = exhibitionDao.getStandByAreaId(areaId);
        map.put("stand", stand);
        return map;
    }

    /**
     * 查看展位是否已经入驻
     *
     * @param standId
     */
    public void verifyStand(Integer standId) {
        Integer participantId;
        try {
            participantId = exhibitionDao.getParticipantIdByStandId(standId);
            if (participantId != null) {
                throw new RuntimeException("展位已经入驻");
            }
        } catch (Exception e) {
            throw new RuntimeException("展位已经入驻");
        }
    }


    /**
     * 上传参展文件
     *
     * @param participantId
     * @param key
     * @param fileType
     */
    public void uploadParticipantFile(Integer participantId, String key, String fileName, String fileType, Integer userId) {

        try {
            exhibitionDao.saveParticipantFile(userId, participantId, key, fileName, fileType, ExpoConstants.PARTICIPANT_FILE);
        } catch (Exception e) {
            throw new RuntimeException("上传失败");
        }
    }

    /**
     * 根据展馆id分页显示参展商信息
     *
     * @param hallId
     * @return
     */
    public Integer countParticipantByHallId(Integer hallId) {
        return exhibitionDao.findParticipantByHallId(hallId);
    }

    /**
     * 根据展馆id分页获取参展商信息
     *
     * @param hallId
     * @param page
     * @param limit
     * @return
     */
    public List<Participants> getParticipantList(Integer hallId, Integer page, Integer limit) {
        if (page < 1 || limit < 1) {
            throw new RuntimeException("请输入正确的页码和每页个数");
        }
        Integer offset = (page - 1) * limit;
        List<Participants> list = exhibitionDao.getParticipantList(hallId, offset, limit);
        return list;
    }

    /**
     * 主办方修改参展商展位
     *
     * @param m
     */
    public void modifyParticipantStandByManager(@Valid ModifyStandInfoDto m) {
        Integer participantId;
        try {
            participantId = exhibitionDao.getParticipantIdByStandId(m.getStandId());
        } catch (Exception e) {
            throw new RuntimeException("修改展位失败，有展位分配给了多个参展商");
        }
        if (participantId != null) {
            throw new RuntimeException("展位已经有参展商入驻,请选择其他展位");
        }
        try {
            exhibitionDao.modifyParticipantStandByManager(m);
        } catch (Exception e) {
            throw new RuntimeException("修改展位失败");
        }
    }

    /**
     * 修改展会基本信息
     *
     * @param u
     */
    public void modifyExpo(UploadExpoDto u, Integer userId) {
    }

    /**
     * 取消参展商
     */
    public void cancelParticipant(Integer participantId) {
        try {
            exhibitionDao.cancelParticipant(participantId);
        } catch (Exception e) {
            throw new RuntimeException("取消失败");
        }
    }

    /**
     * 根据展商id获取展商基本信息
     *
     * @param expoId
     * @return
     */
    public ExhibitionDto getExpoBaseInfo(Integer expoId) {
        return exhibitionDao.getDetail(expoId);
    }

    /**
     * 根据展会id获取展会的展位信息，嵌套格式
     *
     * @param expoId
     * @return
     */
    public List<HallDto> getStandInfo(Integer expoId) {
        //查询所有展馆的id和名称
        List<HallDto> hallDtos = exhibitionDao.getHallByExpoId(expoId);
        //查询所有的展区的id和名称
        List<AreaDto> areaDtos = exhibitionDao.getAreaDtoByExpoId(expoId);
        //获取所有的展位编号和开始数字,结束数字
        List<StandDto> standDtos = exhibitionDao.getStandDto(expoId);
        //遍历展馆
        for (HallDto h : hallDtos) {
            //遍历展区
            for (AreaDto a : areaDtos) {
                //将展区加到展馆中
                if (a.getHallId().equals(h.getId())) {
                    h.getArea().add(a);
                    for (StandDto s : standDtos) {
                        if (s.getAreaId().equals(a.getId())) {
                            //将展位加到展区中
                            a.getStand().add(s);
                        }
                    }
                }

            }
        }
        return hallDtos;
    }

    /**
     * 返回历史展会信息
     *
     * @param expoId
     * @return
     */
    public Map<String, Object> getExpoHistory(Integer expoId) {
        Map<String, Object> map = new HashMap<>();
        List<ExhibitionHistory> list = exhibitionDao.getHisExhibition(expoId);
        map.put("hisExpo", list);
        return map;
    }

    /**
     * 返回展会下的活动
     *
     * @param expoId
     * @return
     */
    public Map<String, Object> getExpoActivity(Integer expoId) {
        Map<String, Object> map = new HashMap<>();
        List<ExhibitionActivity> list = exhibitionDao.getActivity(expoId);
        map.put("expoActivity", list);
        return map;
    }

    /**
     * 修改历史展会
     *
     * @param e
     */
    @Transactional(rollbackFor = Exception.class)
    public void modifyHisExpo(@Valid ExpoHistoryDto e) {
        try {
            //删除原有展会
            exhibitionDao.delHisExpoByExpoId(e.getExpoId());
        } catch (Exception e1) {
            throw new RuntimeException("修改失败");
        }

        for (ExhibitionHistory eh : e.getExpoHistory()) {
            try {
                //插入最新展会
                exhibitionDao.insertExhibitionHistory(e.getExpoId(), eh.getHisExpoName(), eh.getHisExpoUrl(), eh.getHisExpoId(), new Date(), new Date());
            } catch (Exception e1) {
                throw new RuntimeException("修改失败");
            }
        }
    }

    /**
     * 更新展会活动
     *
     * @param e
     */
    @Transactional(rollbackFor = Exception.class)
    public void modifyExpoActivity(@Valid ExpoActivityDto e) {
        try {
            exhibitionDao.delExpoActivity(e.getExpoId());
        } catch (Exception e1) {
            throw new RuntimeException("修改失败");
        }
        for (ExhibitionActivity ea : e.getActivityList()) {
            try {
                exhibitionDao.insertExhibitionActivity(e.getExpoId(), ea.getActivityName(), ea.getActivityUrl(), ea.getActivityId(), new Date(), new Date());
            } catch (Exception e1) {
                throw new RuntimeException("修改失败");
            }

        }
    }


    /**
     * 根据展会id获取展会的验证码，使用验证码的用户id信息
     *
     * @param expoId
     * @return
     */
    private List<Stand> getStandVerifyCode(Integer expoId) {
        return exhibitionDao.getStandInfo(expoId);
    }

    /**
     * 更新展位信息
     *
     * @param expoDto  更新的展位信息
     * @param stands   修改前验证码的信息
     * @param oldScale 修改前展位的数量
     */
    @Transactional
    public void updateStandInfo(ExpoDto expoDto, List<Stand> stands, Integer oldScale) {
    }

    /**
     * 获得参展商详情
     *
     * @param participantId
     * @return
     */
    public ParticipantDetailDto getParticipantDetail(Integer participantId) {
        try {
            return exhibitionDao.getParticipantDetail(participantId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("返回失败");
        }
    }


    /**
     * 获得用户的参展商信息列表
     *
     * @param id
     * @param status
     * @return
     */
    public List<ParticipantListDto> getParticipantsList(Integer id, String status) {
        try {
            return exhibitionDao.getParticipantListByUserId(id, status);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("返回失败");
        }
    }

    /**
     * 修改参展商信息
     *
     * @param participantDetailDto
     */
    public void modifyParticipant(ParticipantDetailDto participantDetailDto) {
        try {
            exhibitionDao.modifyParticipant(participantDetailDto);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("修改失败");
        }

    }

    /**
     * 删除参展商文件
     *
     * @param fileId
     */
    public void delParticipantFile(Integer fileId, Integer userId) {
        Integer Id;
        try {
            Id = exhibitionDao.findUserIdByFileId(fileId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("请勿违规操作");
        }
        if (!Id.equals(userId)) {
            throw new RuntimeException("请勿违规操作");
        }
        try {
            exhibitionDao.delParticipantFile(fileId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("删除失败");
        }

    }

    /**
     * 获得参展商文件
     *
     * @param participantId
     * @return
     */
    public List<FileManage> getParticipantFile(Integer participantId) {
        try {
            List<FileManage> list = exhibitionDao.getParticipantFile(participantId);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("返回失败");
        }
    }


    /**
     * 主办方删除展馆信息
     *
     * @param expoId
     * @param hallId
     */
    public void delHall(Integer expoId, Integer hallId) {
        try {
            exhibitionDao.delHall(expoId, hallId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("删除失败");
        }
    }

    public Integer getExpoIdByName(String name) {
        return exhibitionDao.getExpoIdByName(name);
    }

    public Exhibition findExpoById(Integer id) {
        return exhibitionDao.findExpoById(id);
    }
}
